﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace canyinguanlixitong
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      string str = "Data Source=.;Initial Catalog=Supermarket management system;Integrated Security=True";
      int danjuhao;
     
      
        private void button5_Click(object sender, EventArgs e)
        {

            if (this.comboBox2.SelectedItem != null)
            {
                this.button7.Enabled = true;
             
                this.button3.Enabled = true;
                this.button6.Enabled = true;
                this.button2.Enabled = true;
                frm添加采购 frm = new frm添加采购();
                frm.ShowDialog();
                SqlConnection conn = new SqlConnection(str);
                conn.Open();
                DataSet ds = new DataSet("Student");
                string sql = @"select id,commid,danwei,danjuhaoid,spName,spJia,sum from dbo.sangping,dbo.单据号,dbo.commodity
where dbo.sangping.单据号=dbo.单据号.danjuhaoid and dbo.commodity.spID=dbo.sangping.commid
and danjuhaoid='"+danjuhao+"'";
                SqlDataAdapter asdf = new SqlDataAdapter(sql,conn);
                asdf.Fill(ds,"Student");
                this.dataGridView1.DataSource = ds.Tables["Student"];

            }
            else
            {
                MessageBox.Show("请选择仓库");
            }
           
        }


        public void chaxun() {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            DataSet ds = new DataSet("Student");
            string sql = @"select id,commid,danwei,danjuhaoid,spName,spJia,sum from dbo.sangping,dbo.单据号,dbo.commodity
where dbo.sangping.单据号=dbo.单据号.danjuhaoid and dbo.commodity.spID=dbo.sangping.commid
and danjuhaoid='" + danjuhao + "'";
            SqlDataAdapter asdf = new SqlDataAdapter(sql, conn);
            asdf.Fill(ds, "Student");
            this.dataGridView1.DataSource = ds.Tables["Student"];
        }
        private void button1_Click(object sender, EventArgs e)
        {
            frm供货商 frm = new frm供货商();
            frm.ShowDialog();
            this.txtname.Text = frm.stu;
        }
        //修改
        private void btnsahnchu_Click(object sender, EventArgs e)
        {
            
        }
       

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult reader = MessageBox.Show("确定删除吗", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (this.dataGridView1.SelectedCells.Count > 0)
            {
                string qwe = this.dataGridView1.SelectedCells[0].Value.ToString();
              //  MessageBox.Show(qwe);
                if (reader == DialogResult.OK)
                {
                    SqlConnection conn = new SqlConnection(str);
                    conn.Open();
                    try
                    {
                        string sql = "delete dbo.sangping where id='" + qwe + "'";
                        SqlCommand comm = new SqlCommand(sql, conn);
                        int i = comm.ExecuteNonQuery();
                        if (i > 0)
                        {
                            MessageBox.Show("删除成功");
                            chaxun();
                        }
                        else
                        {
                            MessageBox.Show("重新选择");

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            string sql1 = @"select danjuhaoid from dbo.单据号";
            SqlCommand com1 = new SqlCommand(sql1, conn);
            SqlDataReader rea1 = com1.ExecuteReader();
            while (rea1.Read())
            {
                danjuhao = Convert.ToInt32(rea1["danjuhaoid"]) + 1;
            }
         //   MessageBox.Show(danjuhao + "");

            rea1.Close();
        }
        //保存
        private void button7_Click(object sender, EventArgs e)
        {
            MessageBox.Show("保存成功","温馨提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            this.Close();
        }
        //关闭
        private void button8_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
